from django.db import models
from django.contrib.auth.models import User
from .formatChecker import RestrictedFileField


class MyFile(models.Model):
    media_file = None
    media_name = models.CharField(max_length=512, verbose_name='文件名称', blank=True, help_text='【可选参数】默认为媒体文件名，亦可手动设置')
    media_size = models.CharField(max_length=135, verbose_name='文件大小', blank=True, help_text="【可选参数】默认生成")
    media_creator = models.CharField(max_length=10, verbose_name='创建人', default="雷锋", blank=True,
                                     help_text="【可选参数】默认生成")
    pub_date = models.DateTimeField(auto_now=True, blank=True, help_text="【可选参数】默认生成", verbose_name='提交日期')
    category = models.CharField(max_length=10, blank=True, help_text="【必填参数】默认为专升本资料", default="专升本资料",
                                verbose_name='资源分类')

    def __str__(self):
        return self.media_name

    @property
    def filesize(self):
        """
        对管理界面均生效
        """

        fileSize = '无法计算'
        x = self.media_file.size
        y = 512000
        if x < y:
            value = round(x / 1024, 2)
            fileSize = f'{value} KB'
        elif x < y * 1024:
            value = round(x / (1024 * 1024), 2)
            fileSize = f'{value} MB'
        else:
            value = round(x / (1024 * 1024 * 1024), 2)
            fileSize = f'{value} GB'
        return fileSize


class Media(MyFile):
    media_file = RestrictedFileField(upload_to='files/', blank=True, null=True, max_length=500, verbose_name='媒体文件',
                                     help_text='【必填参数】请选择上传文件',
                                     content_types=['application/pdf', 'application/vnd.openxmlformats-officedocument'
                                                                       '.spreadsheetml.sheet', 'application/msword',
                                                    'text/plain', 'text/csv'], max_upload_size=5242880)
    link = models.CharField(max_length=100, blank=True, null=True, default='/media/files/',
                            help_text='【必填参数】默认存本站，亦可手动替换为网盘链接', verbose_name='资源链接')


class Announcement(models.Model):
    def __str__(self):
        return self.title

    title = models.CharField(max_length=100)
    link = models.CharField(max_length=100, blank=True, default='announcement')
    pub_date = models.DateTimeField('date published')
    content = models.TextField(max_length=2000, blank=True, null=True)


class NativeImage(MyFile):
    media_file = RestrictedFileField(upload_to='img/', blank=True, null=True,
                                     content_types=['image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'image/jpg'],
                                     max_upload_size=5242880, verbose_name='图片文件', help_text="请选择图片文件")

    @property
    def filesize(self):
        """
        对管理界面均生效
        """

        ext = '无法计算'
        x = self.media_file.size
        y = 512000
        if x < y:
            value = round(x / 1024, 2)
            ext = f'{value} KB'
        elif x < y * 1024:
            value = round(x / (1024 * 1024), 2)
            ext = f'{value} MB'
        else:
            value = round(x / (1024 * 1024 * 1024), 2)
            ext = f'{value} GB'
        return ext


class Score(models.Model):
    def __str__(self):
        return self.name

    name = models.CharField(max_length=6, verbose_name="姓名")
    pub_date = models.DateTimeField(verbose_name="提交时间")
    total = models.IntegerField(verbose_name="刷题数量")
    false = models.IntegerField(verbose_name="答错的题数")
    true = models.IntegerField(verbose_name="答对的题数", default=0)
    subject = models.CharField(max_length=6, verbose_name="考试科目", default="思想政治")
